package com.example.cloud.utils;

import com.example.cloud.constants.CommonConstants;
import com.example.cloud.model.common.CommonModel;
import com.example.cloud.model.system.User;
import com.example.cloud.socket.WebSocketServer;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.websocket.EncodeException;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.example.cloud.utils.DateUtils.getDateFormat;

/**
 * Created by wangtao on 2018/3/13.
 */
public class BusinessUtil {

    /**
     * 公共属性绑定
     * @param model
     */
    public static void appendRecord(CommonModel model) {
        User user = RequestUtil.getLoginUser();
//        if(model.getCreationTime() != null){
//            if(user != null){
//                model.setUpdatedBy(user.getUsername());
//            }
//            model.setUpdateTime(new Date());
//            model.setUpdateMethod("system");
//        }else{
            if(user != null){
                model.setCreatedBy(user.getUsername());
            }
            model.setCreationTime(new Date());
            model.setCreationMethod("system");
            
            model.setUpdateTime(new Date());
            model.setUpdateMethod("system");
//            model.setUpdateMethod(null);
//           model.setUpdateTime(null);
            model.setUpdatedBy(user.getUsername());
//            model.setUpdatedBy(null);
            model.setDeleteFlag(0);
        //}
    }

    /**
     * 根据出生日期，计算出在某一个日期的年龄
     *
     * @param birthday Date 出生日期时间对象
     * @param date2 Date 计算日期对象
     * @return int 返回date2那一天出生日期为birthday的年龄，如果birthday大于date2则返回-1
     */
    public static int getAge(Date birthday, Date date2) {
        if (null == birthday || null == date2) {
            return -1;
        }

        if (birthday.after(date2)) { // birthday大于date2
            return -1;
        }

        int ibdYear = StringUtils.getInt(getDateFormat(birthday, "yyyy"), -1);
        int idate2Year = StringUtils.getInt(getDateFormat(date2, "yyyy"), -1);

        if (ibdYear < 0 || idate2Year < 0) {
            return -1;
        }
        if (ibdYear > idate2Year) {
            return -1;
        }

        return idate2Year - ibdYear + 1;
    }

    /**
     * 属性绑定
     * @param obj
     * @param source
     * @param clazz
     */
    public static void convertValue(Object obj, Object source, Class clazz){
        ObjectMapper mapper = new ObjectMapper();
        obj = mapper.convertValue(source, clazz);
    }

    /**
     * 获取当前时间所在周的第一天
     * @param date
     * @return
     * @throws ParseException
     */
    public static String getFirstOfWeek(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(DateUtils.parseDate(date, CommonConstants.DATE_FORMATS));
        int d = 0;
        if(cal.get(Calendar.DAY_OF_WEEK)==1){
            d = -6;
        }else{
            d = 2-cal.get(Calendar.DAY_OF_WEEK);
        }
        cal.add(Calendar.DAY_OF_WEEK, d);
        //所在周开始日期
        return sdf.format(cal.getTime()) + " 00:00:00";
    }

    /**
     * 获取当前时间所在周的最后一天
     * @param date
     * @return
     * @throws ParseException
     */
    public static String getLastOfWeek(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(org.apache.commons.lang.time.DateUtils.parseDate(date, CommonConstants.DATE_FORMATS));
        int d = 0;
        if(cal.get(Calendar.DAY_OF_WEEK)==1){
            d = -6;
        }else{
            d = 2-cal.get(Calendar.DAY_OF_WEEK);
        }
        cal.add(Calendar.DAY_OF_WEEK, d);
        cal.add(Calendar.DAY_OF_WEEK, 6);
        //所在周开始日期
        return sdf.format(cal.getTime()) + " 23:59:59";
    }

    /**
     * 发送消息
     * @param type
     * @param message
     */
    public static void sendMessage(String message, List<Long> clientIds, int type){
        try {
            WebSocketServer.sendInfo(message, clientIds, type);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (EncodeException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
